package net.jsoft.platform.security.entity.user;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fasterxml.jackson.annotation.JsonIgnore;

import net.jsoft.pms.entity.dept.Dept;

@Entity
@Table(name = "t_security_user") //code11  
public class SysUser implements java.io.Serializable, UserDetails
{

    /**
     * 
     */
    private static final long serialVersionUID = 4623586982716952475L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private long id;
    @Column(name = "name", length = 120)
    private String name; //用户名  
    @Column(name = "email", length = 50)
    private String email;//用户邮箱  
    @Column(name = "password", length = 120)
    private String password;//用户密码  
    @Temporal(TemporalType.DATE)
    @Column(name = "dob", length = 10)
    private Date dob;//时间  
    @Column(name = "phone", length = 50)
    private String phone;
    @Column(name = "fullName", length = 200)
    private String fullName;

    private String indexPage;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "dept")
    private Dept dept;

    private String wechatCode;

    private String qqCode;

    private String sinaCode;

    private String oschinaCode;

    private String githubCode;

    private String fbCode;

    private String twitterCode;

    public String getIndexPage()
    {
        return indexPage;
    }

    public void setIndexPage(String indexPage)
    {
        this.indexPage = indexPage;
    }

    @JsonIgnore
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "t_security_user_role", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
    private Set<SysRole> sysRoles = new HashSet<SysRole>(0);// 所对应的角色集合  

    public SysUser()
    {
    }

    public SysUser(String name, String email, String password, Date dob, Set<SysRole> SysRoles)
    {
        this.name = name;
        this.email = email;
        this.password = password;
        this.dob = dob;
        this.sysRoles = SysRoles;
    }

    public Dept getDept()
    {
        return dept;
    }

    public void setDept(Dept dept)
    {
        this.dept = dept;
    }

    public long getId()
    {
        return this.id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public String getName()
    {
        return this.name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getEmail()
    {
        return this.email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public String getPassword()
    {
        return this.password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public Date getDob()
    {
        return this.dob;
    }

    public void setDob(Date dob)
    {
        this.dob = dob;
    }

    public Set<SysRole> getSysRoles()
    {
        return sysRoles;
    }

    public void setSysRoles(Set<SysRole> sysRoles)
    {
        this.sysRoles = sysRoles;
    }

    public String getPhone()
    {
        return phone;
    }

    public void setPhone(String phone)
    {
        this.phone = phone;
    }

    public String getFullName()
    {
        return fullName;
    }

    public void setFullName(String fullName)
    {
        this.fullName = fullName;
    }

    //    @Override
    //    public String toJSONString()
    //    {
    //
    //        return "{id:\"" + id + "\", name:\"" + name + "\", email:\"" + email + "\", password:\"" + password + "\", dob:\"" + dob + "\", phone:\"" + phone + "\", fullName:\"" + fullName + "\", dept:\""
    //                + dept + "\"}";
    //    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities()
    {

        Collection<GrantedAuthority> authorities = new ArrayList<>();
        Set<SysRole> userRoles = this.getSysRoles();

        if (userRoles != null)
        {
            for (SysRole role : userRoles)
            {
                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getName());
                authorities.add(authority);
            }
        }
        return authorities;
    }

    @Override
    public String getUsername()
    {
        return this.getName();
    }

    @Override
    public boolean isAccountNonExpired()
    {
        return true;
    }

    @Override
    public boolean isAccountNonLocked()
    {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired()
    {
        return true;
    }

    @Override
    public boolean isEnabled()
    {
        return true;
    }

    public String getWechatCode()
    {
        return wechatCode;
    }

    public void setWechatCode(String wechatCode)
    {
        this.wechatCode = wechatCode;
    }

    public String getQqCode()
    {
        return qqCode;
    }

    public void setQqCode(String qqCode)
    {
        this.qqCode = qqCode;
    }

    public String getSinaCode()
    {
        return sinaCode;
    }

    public void setSinaCode(String sinaCode)
    {
        this.sinaCode = sinaCode;
    }

    public String getOschinaCode()
    {
        return oschinaCode;
    }

    public void setOschinaCode(String oschinaCode)
    {
        this.oschinaCode = oschinaCode;
    }

    public String getGithubCode()
    {
        return githubCode;
    }

    public void setGithubCode(String githubCode)
    {
        this.githubCode = githubCode;
    }

    public String getFbCode()
    {
        return fbCode;
    }

    public void setFbCode(String fbCode)
    {
        this.fbCode = fbCode;
    }

    public String getTwitterCode()
    {
        return twitterCode;
    }

    public void setTwitterCode(String twitterCode)
    {
        this.twitterCode = twitterCode;
    }

}